package com.solome.ui;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Disposable;

public class SoScrollView extends ScrollPane implements Disposable {
	public enum SoScrollType {
		Horizontal, Vertical
	}

	// static group = null;

	public static synchronized SoScrollView create(SoScrollType type) {
		if (type == null) {
			throw new IllegalArgumentException("SoScrollType cannot be null.");
		}
		WidgetGroup group = null;
		switch (type) {
		case Horizontal:
			group = new HorizontalGroup();
			break;
		case Vertical:
			group = new VerticalGroup();
			break;
		default:
			throw new IllegalArgumentException(
					"SoScrollType cannot be error or \"\".");
		}
		SoScrollView instance = new SoScrollView(group);
		return instance;
	}

	public static synchronized SoScrollView create(SoScrollType type, Skin skin) {
		if (type == null) {
			throw new IllegalArgumentException("SoScrollType cannot be null.");
		}
		WidgetGroup group = null;
		switch (type) {
		case Horizontal:
			group = new HorizontalGroup();
			break;
		case Vertical:
			group = new VerticalGroup();
			break;
		default:
			throw new IllegalArgumentException(
					"SoScrollType cannot be error or \"\".");
		}
		SoScrollView instance = new SoScrollView(group, skin);
		return instance;
	}

	public SoScrollView(Actor widget) {
		super(widget);
	}

	public SoScrollView(Actor widget, Skin skin) {
		super(widget, skin);
	}

	public void addChild(Object object) {
		this.addChild(object, 0);
	}

	public void addChild(Object object, int tag) {
		WidgetGroup group = (WidgetGroup) getWidget();

		if (object instanceof Actor) {
			Actor actor = (Actor) object;
			actor.setName("" + tag);
			group.addActor((Actor) object);
		} /*
		 * else if (object instanceof Action) { // Action action = (Action)
		 * object; group.addAction((Action) object); }
		 */else if (object instanceof Texture) {
			Image image = new Image((Texture) object);
			image.setName("" + tag);
			group.addActor(image);
		} else
			throw new IllegalStateException("Object error.");
	}

	public void removeChild(Action action, int tag) {
		WidgetGroup group = (WidgetGroup) getWidget();
		Actor actor = group.findActor("" + tag);
		this.removeActor(actor);
	};

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		WidgetGroup group = (WidgetGroup) getWidget();
		for (Actor actor : group.getChildren()) {
			if (actor instanceof Disposable) {
				Disposable disposable = (Disposable) actor;
				disposable.dispose();
			}
		}

		for (Action action : group.getActions()) {
			if (action instanceof Disposable) {
				Disposable disposable = (Disposable) action;
				disposable.dispose();
			}
		}

		group.clearActions();
		group.clearChildren();
		group.clearListeners();

		group.clear();
	}
}
